package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.*;
import com.example.demo.tools.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RechargeService rechargeService;

    @Autowired
    private BrowseService browseService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private BillService billService;

    @Autowired
    private ReckoningService reckoningService;

    @GetMapping("/select")
    public Result select() {
        List<User> users = userService.selectUser();
        return ResultFactory.buildSuccessResult(users);
    }

    @PostMapping("/selectBI/{id}")
    public Result selectByName(@PathVariable("id") String id){
        try {
            System.out.println(id);
            User user = userService.selectById1(id);
            String pw = AES.code1(user.getPassWord());
            user.setPassWord(pw);
            return ResultFactory.buildSuccessResult(user);
        }catch (Exception e){
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/recharge")
    @ResponseBody
    public Result recharge(@RequestBody Map<String, String> map) {
        try {
            String pw = AES.code(map.get("PassWord"));
            String id = map.get("Id");
            String passwd = userService.selectById(id);
            if (!passwd.equals(pw))
                return ResultFactory.buildFailResult("密码错误!");
            Recharge recharge = new Recharge();
            recharge.setCustomerId(id);
            recharge.setSumOM(Integer.parseInt(map.get("Money")));
            recharge.setDate(map.get("Date"));
            userService.updateBalance1(map.get("Money"), id);
            rechargeService.insertRecharge(recharge);
            return ResultFactory.buildSuccessResult(id);
        } catch (Exception e) {
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/change")
    @ResponseBody
    public Result change(@RequestBody Map<String, String> map) {
        try {
            String pw = AES.code(map.get("oldPw"));
            String id = map.get("Id");
            String passwd = userService.selectById(id);
            if (!passwd.equals(pw))
                return ResultFactory.buildFailResult("密码错误!");
            String pw1 = AES.code(map.get("newPw"));
            userService.updatePw(pw1, id);
            return ResultFactory.buildSuccessResult(id);
        } catch (Exception e) {
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @GetMapping("/selectRecharge")
    public Result selectRecharge() {
        List<Recharge> recharges = rechargeService.selectRecharge();
        return ResultFactory.buildSuccessResult(recharges);
    }

    @PostMapping("/selectRecharge1/{id}")
    public Result selectRecharge1(@PathVariable("id") String id) {
        List<Recharge> recharges = rechargeService.selectRecharge1(id);
        return ResultFactory.buildSuccessResult(recharges);
    }

    @PostMapping("/selectRechargeByY/{id}")
    public Result selectRechargeByY(@PathVariable("id") String id) {
        List<Recharge1> recharge1s = rechargeService.selectRechargeByY(id);
        return ResultFactory.buildSuccessResult(recharge1s);
    }

    @PostMapping("/selectRechargeByD/{id}")
    public Result selectRechargeByD(@PathVariable("id") String id) {
        List<Recharge1> recharge1s = rechargeService.selectRechargeByD(id);
        return ResultFactory.buildSuccessResult(recharge1s);
    }

    @PostMapping("/deleteRecharge/{id}")
    public Result deleteRecharge(@PathVariable("id") String id) {
        rechargeService.deleteRecharge(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/updateU")
    @ResponseBody
    public Result updateU(@RequestBody Map<String, String> map) {
        try {
            User user = new User();
            user.setCustomerId(map.get("customerId"));
            String pw = AES.code(map.get("passWord"));
            user.setPassWord(pw);
            user.setUserName(map.get("userName"));
            user.setPhoneNumber(map.get("phoneNumber"));
            user.setAddress(map.get("address"));
            user.setBalance(map.get("balance"));
            user.setPower(Integer.parseInt(map.get("power")));
            userService.updateUser(user);
            return ResultFactory.buildSuccessResult("1");
        } catch (Exception e) {
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/updateU1")
    @ResponseBody
    public Result updateU1(@RequestBody Map<String, String> map) {
        try {
            User user = new User();
            user.setCustomerId(map.get("CustomerId"));
            user.setUserName(map.get("UserName"));
            user.setPhoneNumber(map.get("PhoneNumber"));
            user.setAddress(map.get("Address"));
            userService.updateUserInfo(user);
            return ResultFactory.buildSuccessResult("1");
        } catch (Exception e) {
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/delU/{id}")
    public Result delU(@PathVariable("id") String id) {
        userService.delUser(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/addU")
    @ResponseBody
    public Result addU(@RequestBody Map<String, String> map) {
        try {
            User user = new User();
            user.setCustomerId(map.get("CustomerId"));
            User user1 = userService.selectById1(user.getCustomerId());
            if (user1 != null)
                return ResultFactory.buildFailResult("用户已存在!");
            String pw = AES.code(map.get("PassWord"));
            user.setPassWord(pw);
            user.setUserName(map.get("UserName"));
            user.setPhoneNumber(map.get("PhoneNumber"));
            user.setAddress(map.get("Address"));
            user.setBalance(map.get("Balance"));
            user.setPower(Integer.parseInt(map.get("Power")));
            userService.addUser(user);
            return ResultFactory.buildSuccessResult("1");
        } catch (Exception e) {
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/browse")
    public void browse(@RequestBody Map<String, String> map) throws ParseException {
        Browse browse = new Browse();
        browse.setCustomerId(map.get("customerId"));
        browse.setBookName(map.get("bookName"));
        browse.setPrice(map.get("price"));
        browse.setDate(map.get("date"));
        browseService.insert(browse);
    }

    @PostMapping("/selectBrowse/{id}")
    public Result selectBrowse(@PathVariable("id") String id){
        List<Browse> browses=browseService.selectBrowse(id);
        return ResultFactory.buildSuccessResult(browses);
    }

    @PostMapping("/deleteBrowse/{id}")
    public Result deleteBrowse(@PathVariable("id") String id) {
        browseService.deleteBrowse(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/selectBrBD")
    public Result selectBrBD(@RequestBody Browse requestBrowse){
        try {
            String id = requestBrowse.getCustomerId();
            String date=requestBrowse.getDate();
            List<Browse> browses=browseService.selectBrBD(id,date);
            return ResultFactory.buildSuccessResult(browses);
        }catch (Exception e){
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/selectLInfo/{id}")
    public Result selectLInfo(@PathVariable("id") String id) {
        List<Login> logins=loginService.selectLInfo(id);
        return ResultFactory.buildSuccessResult(logins);
    }

    @PostMapping("/deleteLInfo/{id}")
    public Result deleteLInfo(@PathVariable("id") String id) {
        loginService.deleteLInfo(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/selectLiBD")
    public Result selectLiBD(@RequestBody Browse requestBrowse){
        try {
            String id = requestBrowse.getCustomerId();
            String date=requestBrowse.getDate();
            List<Login> logins=loginService.selectLiBD(id,date);
            return ResultFactory.buildSuccessResult(logins);
        }catch (Exception e){
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/selectBill/{id}")
    public Result selectBill(@PathVariable("id") String id){
        List<Bill1> bills=billService.selectBill(id);
        return ResultFactory.buildSuccessResult(bills);
    }

    @PostMapping("/deleteBill/{id}")
    public Result deleteBill(@PathVariable("id") String id) {
        billService.deleteBill(id);
        return ResultFactory.buildSuccessResult(id);
    }

    @PostMapping("/selectBiBD")
    public Result selectBiBD(@RequestBody Browse requestBrowse){
        try {
            String id = requestBrowse.getCustomerId();
            String date=requestBrowse.getDate();
            List<Bill1> bills=billService.selectBiBD(id,date);
            return ResultFactory.buildSuccessResult(bills);
        }catch (Exception e){
            return ResultFactory.buildFailResult("错误!");
        }
    }

    @PostMapping("/selectReckoningByY/{id}")
    public Result selectReckoningByY(@PathVariable("id") String id) {
        List<Reckoning> reckonings = reckoningService.selectReckoningByY(id);
        return ResultFactory.buildSuccessResult(reckonings);
    }

    @PostMapping("/selectReckoningByD/{id}")
    public Result selectReckoningByD(@PathVariable("id") String id) {
        List<Reckoning> reckonings = reckoningService.selectReckoningByD(id);
        return ResultFactory.buildSuccessResult(reckonings);
    }
}
